MemoryBlock Class
A MemoryBlock object allocates a sequence of bytes in memory and manipulates those bytes directly. A MemoryBlock can be passed in place of a Ptr when used in a Declare call.
More information available in parent classes: Object
Notes
Memoryblocks can be used whenever you need a container for any arbitrary binary data.
Memoryblocks are used when accessing entry points in shared libraries and when making OS calls using the Declare function.
MemoryBlocks supports creating a zero-length MemoryBlock.
The contents of a Memoryblock can be viewed in the REALbasic Debugger.
Examples
The following example reads a real number into a memory block and then displays it:
Dim d as Single
m= NewMemoryBlock(4)
m.singlevalue(0)=123.456
d=m.singlevalue(0)
MsgBox Str(d)
The following example stores a string in a MemoryBlock and displays it:
m.byte(0)=12
m.byte(1)=72
m.byte(2)=101
m.byte(3)=108
m.byte(4)=108
m.byte(5)=111
m.byte(6)=32
m.byte(7)=87
m.byte(8)=111
m.byte(9)=114
m.byte(10)=108
m.byte(11)=100
m.byte(12)=33
MsgBox m.pstring(0)
To read the string using CString, add the terminating byte before the call, i.e.,
This example backs a BinarySream with a MemoryBlock that is declared 0-sized.
Dim bs as New BinaryStream(mb)
bs.WriteLong(4)
bs.WriteDouble(3.14)
bs.Close
MsgBox Str(mb.Long (0))
See Also
NewMemoryBlock function.